function test_gauss(item, ntimes)
% A simple function to test the correctness of Gauss implementation
%
%   - test_gauss(item, ntimes)
%
%     available items:
%       - 'full'
%       - 'diag'
%       - 'iso'       
%    
% Created by Dahua Lin, on Dec 25, 2007
%

%% main

switch item
    case 'full'
        repeat_test('full gauss construction test', @test_full, ntimes);
    case 'diag'
        repeat_test('diag gauss construction test', @test_diag, ntimes);
    case 'iso'
        repeat_test('iso gauss construction test', @test_iso, ntimes);
    otherwise
        error('Unknown item %s', item);
end


%% repeating function

function repeat_test(f, ntimes)

for i = 1 : ntimes
    disp(['Iter ', int2str(i)]);
    f();
    disp(' ');
end

    


%% testing functions

function test_full()

d = 3;
K = 5;

muK = rand(d, K);
mu1 = muK(:, 1);

sms = cell(1, K);
for i = 1 : K
    sms{i} = slrandsym(rand(1, d) + 1.0);
end
smK = cat(3, sms{:});
sm1 = smK(:,:, 1);

disp('Test normal single model');
mdl_1n = slcfullgauss(mu1, sm1);
test_cache_changes(mdl_1n, 2, false);

disp('Test normal multiple models');
mdl_Kn = slcfullgauss(muK, smK);
test_cache_changes(mdl_Kn, 2, false);

disp('Test shared single model');
mdl_1s = slcfullgauss(mu1, {sm1});
test_cache_changes(mdl_1s, 2, true);

disp('Test shared multiple models');
mdl_Ks = slcfullgauss(muK, {sm1});
test_cache_changes(mdl_Ks, 2, true);


function test_diag()

d = 3;
K = 5;

muK = rand(d, K);
mu1 = muK(:, 1);
smK = rand(d, K);
sm1 = smK(:, 1);

disp('Test normal single model');
mdl_1n = slcdiaggauss(mu1, sm1);
test_cache_changes(mdl_1n, 2, false);

disp('Test normal multiple models');
mdl_Kn = slcdiaggauss(muK, smK);
test_cache_changes(mdl_Kn, 2, false);

disp('Test shared single model');
mdl_1s = slcdiaggauss(mu1, {sm1});
test_cache_changes(mdl_1s, 2, true);

disp('Test shared multiple models');
mdl_Ks = slcdiaggauss(muK, {sm1});
test_cache_changes(mdl_Ks, 2, true);



%% Supporting functions


function b = test_cache_changes(mdl, maxlevel, covshared)

b = false;

tol = 1e-12;

if ~check_cache_consistency(mdl, tol, 'quiet')
    disp('Error happened when testing the cache consistency of constructed model');
end

for i = 0 : maxlevel
    for j = 0 : maxlevel
        mdl_src = cache(mdl, i, covshared);
        mdl_dst = cache(mdl_src, j, covshared);
        
        if ~check_cache_consistency(mdl_dst, tol, 'quiet')
            disp(sprintf('Error happened when adjusting cache from level %d to level %d', i, j));
            return;
        end
    end
end

b = true;
return;











